package com.maben.udp.test003.netty_client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
 * 客户端启动类
 */
public class MainClient {
    public static void main(String[] args) throws Exception {
        final Client client = new Client(9099);
        //启动客户端
        new Thread(() -> {
            try {
                client.run();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        Channel channel = null;
        boolean isStart = false;
        while (!isStart) {
            TimeUnit.SECONDS.sleep(1);
            if (null != client.getChannel()) {
                channel = client.getChannel();
                isStart = true;
            }
        }
        String testStr = "hello,xxx";
        ByteBuf byteBuf = Unpooled.wrappedBuffer(testStr.getBytes());
        channel.writeAndFlush(byteBuf);
        //我们通过控制台输入来给服务端发送消息
        for (int i = 0; i < 10 ; i++) {
            Scanner scanner = new Scanner(System.in);
            String text = scanner.nextLine();
            channel.writeAndFlush(Unpooled.wrappedBuffer(text.getBytes()));
        }
    }
}
